//
//  ClassViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/9.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "ClassViewController.h"
#import "ClassForCollectionViewCell.h"
#import "ClassCollectionReusableView.h"
#import "ClassPageViewController.h"
#import "SearchViewController.h"

#define itemWidth (SCREEN_W-60)/5.0
#define itemHeight itemWidth/(100/140.0)+55
@interface ClassViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewLayout* layout;
@property (strong, nonatomic) NSMutableArray * dataArray;

@end

@implementation ClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    NSArray * classArr = [NSArray arrayWithObjects:@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢", nil];
    for (int i=0; i<classArr.count; i++) {
        ClassModel * model = [[ClassModel alloc]init];
        model.name = [classArr objectAtIndex:i];
        model.class_id = [NSString stringWithFormat:@"%d",i];
        [self.dataArray addObject:model];
    }
    [self loadUI];
    NSLog(@"################%@",_class_id);
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.public_headView.hidden = YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}
#pragma mark - 适配
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.collectionView.frame = CGRectMake(0, 64, SCREEN_W, SCREEN_H-64-64);
    [_collectionView reloadData];
}
#pragma mark - 懒加载

- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
        
    }
    return _dataArray;
}

-(void)loadUI
{
    
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    layout. minimumInteritemSpacing  = 10;
    // 3.设置item块的大小 (可以用于自适应)
    layout.estimatedItemSize = CGSizeMake(itemWidth, itemHeight);
    // 设置滑动的方向 (默认是竖着滑动的)
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    // 设置item的内边距
    layout.sectionInset = UIEdgeInsetsMake(1,10,1,10);
    //设置collectionView头视图的大小
    [layout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width,20)];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H-64-64) collectionViewLayout:layout];
    _collectionView.backgroundColor = TAB_BAR_COLOR;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ClassForCollectionViewCell class] forCellWithReuseIdentifier:@"ClassForCollectionViewCell"];
    [self.collectionView registerClass:[ClassCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ClassCollectionReusableView"];
    [_collectionView reloadData];
    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    [_collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark --- UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWidth, itemHeight);
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ClassModel *model = [[ClassModel alloc]init];
    
    ClassCollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ClassCollectionReusableView" forIndexPath:indexPath];
    CGFloat imageW= SCREEN_W;
    CGFloat imageH = SCREEN_W/(2048./600.);
//    headerView.frame = CGRectMake(0, 0, imageW, imageH);
    headerView.Cell_W = imageW;
    headerView.Cell_H = imageH;
    headerView.headType = CollectionHeaderTypeForPhoto;
    model.imageUrl = @"nv_c1";
    [headerView loadHeaderWithModel:model];
    return headerView;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    CGSize size = {SCREEN_W, SCREEN_W/(2048./600.)};
    return size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 需要配置的代码
    //    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    ClassForCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassForCollectionViewCell" forIndexPath:indexPath];
    ClassModel * tmpModel = [_dataArray objectAtIndex:indexPath.row];
    tmpModel.index = indexPath;
    tmpModel.name = _class_id;
    tmpModel.imageUrl = @"nv1";
    cell.backgroundColor = RGBColorAlpha(245, 245, 245, 0.9);
    
    [cell setCellWithModel:tmpModel];
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
